package cn.t.service.device;

import cn.t.core.service.ServiceImpl;
import cn.t.model.device.DeviceDO;
import cn.t.model.device.DeviceOpenRecordDO;
import cn.t.repository.device.DeviceOpenRecordRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 设备开门记录表(DeviceOpenRecord)领域服务
 *
 * @author t
 * @since 2024-05-24 10:19:11
 */
@Slf4j
@Service
@AllArgsConstructor
public class DeviceOpenRecordService extends ServiceImpl<DeviceOpenRecordDO, DeviceOpenRecordRepository> {
    private final DeviceService deviceService;

    public List<DeviceOpenRecordDO> getDeviceOpenRecordByOrderSn(List<String> orderSn) {
        return this.repository.getDeviceOpenRecordByOrderSn(orderSn);
    }

    public List<DeviceOpenRecordDO> findWarning() {
        List<DeviceOpenRecordDO> openRecordDOS = repository.findWarning();
        List<Long> deviceIds = openRecordDOS.stream().map(DeviceOpenRecordDO::getDeviceId).collect(Collectors.toList());
        List<DeviceDO> devices = this.deviceService.getDevices(deviceIds, null, null, null);
        Map<Long, DeviceDO> deviceMap = devices.stream().collect(Collectors.toMap(DeviceDO::getId, Function.identity(), (a, b) -> a));
        openRecordDOS.forEach(f -> {
            if (deviceMap.containsKey(f.getDeviceId())) {
                f.setDeviceName(deviceMap.get(f.getDeviceId()).getDeviceName());
            }
        });
        return openRecordDOS;
    }
    public void updateToSendEmailMark(List<Long> ids) {
        this.repository.updateToSendEmailMark(ids);
    }
}
